<?php
/**
 * Created by Allen.
 * Date: 15-10-12
 * 会员模型
 */

class User_level_model extends HS_Model {


    //获取结果集
    public function getUserLevelList($where=array(), $limit='20', $offset='0', $order_by='id desc')
    {
    	return $this->where($where)->limit($limit, $offset)->order_by($order_by)->find_all();
    }
    
    
    //计算行数
    public function getCount($where = array())
    {
    	return $this->where($where)->count();
    }  
     
    //获取单个
    public function checkUserLevel($where)
    {
    	return $this->where($where)->find();
    }
    
    //删除
    public function deleteUserLevel($where)
    {
    	return $this->where($where)->delete();
    }
    
    //编辑
    public function editUserLevel($where,$data)
    {
    	return $this->where($where)->edit($data);
    }
    
    //添加
    public function addUserLevel($data)
    {
    	if ($this->add($data))
    	{
    		return $this->db->insert_id();
    	}
    }
    /**
     * 查询两个级别的极差百分比
     * @param type $parent_user_level 当前审核通过的会员的父级的父级会员级别
     * @param type $user_level  当前会员的父级
     */
    public function level_rang($parent_user_level,$user_level){
        $user_levels=$this->getUserLevelList(array('status'=>1),100,0,'group_performance asc');
         
        $parent_money=0;
        $is_jia=false;
        if($parent_user_level==$user_level){
            $parent_money=$user_levels[0]['range'];
            $parent_money=round($parent_money,8);
            return $parent_money;
        }else{
			
			 foreach($user_levels as $key=>$value){
            //判断父级的等级是不是比子级的等级高
            if($value['id']==$user_level){
               $is_jia=true;
               continue;
            }
            if($is_jia){
                $parent_money=$parent_money+$value['range'];
            }
            if($value['id']==$parent_user_level){
                $is_jia=false;
                break;
            }
        }
		$parent_money=round($parent_money,8);
        return $parent_money;
		}
       
    }
} 
?>